package com.common方法;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author liyiruo
 * @Description 对二位数组中的某一列 进行排序
 * @Date 2024/4/22 下午4:18
 */

public class Simple {
    public static void main(String[] args) {
        String [][] ts_id = new String[][]{{"1", "2"}, {"99", "0"}, {"876", "9"}, {"3", "100"}};
        //对ts_id二维数组,第2列排序
        String[][] array = compareArray(ts_id, 0);
        System.out.println("排序后===========");
        for (int row = 0; row < array.length; row++) {
            for (int col = 0; col < array[row].length; col++) {
                System.out.print(array[row][col] + " ");
            }
            System.out.println();
        }
    }
    /**
     * @param array 要排序的数组
     * @param n     排第几列
     * @return
     */
    public static String[][] compareArray(String[][] array, final int n) {
        Arrays.sort(array, new Comparator<String[]>() {
            @Override
            public int compare(String [] o1, String[] o2) {
                return (int) (Float.valueOf(o1[n])*100) - (int) (Float.valueOf(o2[n])*100);
            }
        });
        return array;
    }
}

